package top.yxqz.servlet;

import top.yxqz.school.Student;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * @author Mr.余
 * @package top.yxqz.servlet
 * 登录功能测试线程问题
 * 解决方式：
 *  1.将不要共享数据写在方法里面
 *  2.用线程锁synchronized
 */
public class LoginServletDemo2 extends HttpServlet {
    String username = null;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         req.setCharacterEncoding("utf-8");
//        resp.setCharacterEncoding("utf-8");
//        resp.setHeader("Content-Type","text/html;charset=utf-8");
//        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //加一个线程锁
       synchronized (this){
           //        获取前端属性
           username = req.getParameter("xingming");
//            线程休眠3秒钟 单位是毫秒
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        PrintWriter writer = resp.getWriter();
        writer.print("欢迎你:" + username);

       }
    }
}
